/* 
 * File:   main.cpp
 * Author: itu
 *
 * Created on November 25, 2011, 12:19 AM
 */

#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include "FileOperator.h"
#include "LineOperator.h"
#include "Symbol.h"
#include "Terminal.h"
#include "Nonterminal.h"
#include "Rule.h"
#include "Compiler.h"

using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
    Compiler comp;    
    FileOperator fo("file.txt");
    fo.openFileForReading();
    Nonterminal *nonterm;
    Nonterminal *nonterm2;
    Terminal *term;
    Rule *rule;
    
    while(fo.hasNextLine()){        
        LineOperator lo(fo.getLine());
        //cout<<"line: "<<fo.getLine()<<endl;
        nonterm = comp.getNonterminalByIdentifier(lo.getStringBefore("->"));
        //cout<<"nonterm: "<<nonterm->getIdentifier()<<" adres: "<<nonterm<<endl;
        rule = new Rule(nonterm);
        rule->setNumber(comp.countRuleNumber());
        while(lo.isGoodForStreaming() && lo.hasNextString()){
            //cout<<"symbol: "<<lo.getString()<<endl;
                if(isupper(lo.getString().c_str()[0])>0){
                    nonterm2 = comp.getNonterminalByIdentifier(lo.getString());
                    //cout<<"nonterm2: "<<nonterm2->getIdentifier()<<" adres: "<<nonterm2<<endl;
                    rule->addSymbol(nonterm2);
                }else{
                    term = comp.getTerminalByIdentifier(lo.getString());
                    rule->addSymbol(term);
                }        
        }
        nonterm->addRule(*rule);
        /*
        cout<<nonterm->getIdentifier()<<" rules"<<endl;
        nonterm->printRules();
        cout<<"----------"<<endl;
         */
    }
    cout<<endl;
    cout<<"------Terminals-----"<<endl;
    comp.printTerminal();
    cout<<"-----Nonterminals-----"<<endl;
    comp.printNonterminal();
    cout<<"--------Rules-------"<<endl;
    comp.printNonterminalRules();
    cout<<"---Nonterminals first--"<<endl;
    comp.printNonterminalFirst();
    cout<<"---Nonterminals follow--"<<endl;
    comp.printNonterminalFollow();
    cout<<"---DFA olusturuluyor----"<<endl;
    comp.createDFA();
    cout<<"---Ayristirma Tablosu---"<<endl;
    comp.printParserTable();
    cout<<"---Ayristirma Ornekleri--"<<endl;
    comp.parseInput("aabb"); //Doğru giriş
    comp.parseInput("aabbbaabaabb"); //Doğru giriş
    comp.parseInput("aabbb"); //Hatali giriş
    return 0;
}

